package flc.ast.adapter;

import android.graphics.Color;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.chad.library.adapter.base.provider.BaseItemProvider;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.stark.guesstv.lib.module.bean.GuessOption;
import com.stark.guesstv.lib.module.bean.GuessSelOption;

import org.jetbrains.annotations.NotNull;

import java.util.List;

import flc.ast.R;
import stark.common.basic.adaptermutil.StkProviderMultiAdapter;

/**
 * Author：daizhongbin on 2022/2/21
 * Email：1027154338@qq.com
 * Desc: 选中的选项适配器
 */
public class SelOptionAdapter extends StkProviderMultiAdapter<GuessSelOption> {

    public SelOptionAdapter() {
        super(1);
        addItemProvider(new SelOptionItemProvider());
    }

    private int type;//0普通 1正确 2错误

    public void setType(int type) {
        this.type = type;
    }

    /**
     * 填充答案
     *
     * @param option
     */
    public void fillOption(@NonNull GuessOption option) {
        List<GuessSelOption> selOptions = getData();
        for (GuessSelOption selOption : selOptions) {
            if (selOption == null) continue;
            if (selOption.option == null) {
                selOption.option = option;
                break;
            }
        }
        notifyDataSetChanged();
    }

    /**
     * 判断是否已填充满答案选项
     *
     * @return
     */
    public boolean hasFillFull() {
        List<GuessSelOption> selOptions = getData();
        for (GuessSelOption selOption : selOptions) {
            if (selOption == null) continue;
            if (selOption.option == null) {
                return false;
            }
        }
        return true;
    }

    /**
     * 获取填充的答案
     *
     * @return
     */
    public String getSelAnswer() {
        List<GuessSelOption> selOptions = getData();
        StringBuilder builder = new StringBuilder();
        for (GuessSelOption selOption : selOptions) {
            if (selOption == null) continue;
            builder.append(selOption.option.content);
        }
        return builder.toString();
    }

    class SelOptionItemProvider extends BaseItemProvider<GuessSelOption> {

        @Override
        public int getItemViewType() {
            return StkProviderMultiAdapter.TYPE_VIEW;
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_sel_option;
        }

        @Override
        public void convert(@NotNull BaseViewHolder holder, GuessSelOption guessOption) {
            TextView tvOption = holder.getView(R.id.tvOption);
            String text = "";
            if (guessOption.option != null) {
                text = guessOption.option.content;
                tvOption.setBackgroundColor(Color.parseColor("#FFC84D"));
            } else {
                tvOption.setBackgroundColor(Color.parseColor("#271F0C"));
            }
            if (type == 1) {
                tvOption.setBackgroundColor(Color.parseColor("#4DFF61"));
            }else if(type==2){
                tvOption.setBackgroundColor(Color.parseColor("#FF4D4D"));
            }
            tvOption.setText(text);
        }
    }

}
